package com.example.wn.sdu_notification;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class TotalNotification extends Activity {
    Elements article;
    Document doc1 ;
    TextView textView1,textView2,textView3;
    private ProgressDialog progressDialog = null;
    ArrayList<String> list1 = new ArrayList<>();
    String title;
    String author;
    String url;
    Elements urlchild;
    Handler handler = new Handler();
    String urltext;
    //如果有链接，存储链接数
    ArrayList<Integer> arr;
    //如果有链接，存储链接的文本
    ArrayList<String> arr1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_layout);
        textView1 = (TextView) findViewById(R.id.textview1);
        textView2 = (TextView) findViewById(R.id.textview2);
        textView3 = (TextView) findViewById(R.id.textview3);

        doc1 = new Document("");
        article = new Elements();
        urlchild = new Elements();
        arr = new ArrayList<>();
        arr1 = new ArrayList<>();
        urltext = "";

        progressDialog = ProgressDialog.show(TotalNotification.this, "请稍等...", "获取数据中...", true);
        if(MainActivity.newstitle.size() != 0){

            url = "http://www.youth.sdu.edu.cn/news/" + MainActivity.newstitle.get(MainActivity.ps).attr("href");

            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            doc1 = Jsoup.connect(url).get();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        if (doc1.text().length() != 0) {
                            title = doc1.select("#container_head>p").text();
                            author = doc1.select("#author>p").text();
                            article = doc1.select("#text>p");
                            urlchild = doc1.select("#text>p>a");
                        } else {
                            continue;
                        }

                        if (urlchild.size() != 0) {
                            for (int i = 0; i < urlchild.size(); i++) {
                                arr1.add(i, urlchild.get(i).attr("href"));
                            }
                        }

                        if (article.size() != 0) {
                            for (int i = 0; i < article.size(); i++) {
                                list1.add(i, article.get(i).text());
                            }
                        } else {
                            continue;
                        }
                        break;
                    }
                    progressDialog.dismiss();
                    setTextView();
                }
            }).start();
        }
        else{
            url = "http://www.youth.sdu.edu.cn/news/" + MainActivity.hreflist.get(MainActivity.ps);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            doc1 = Jsoup.connect(url).get();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        if (doc1.text().length() != 0) {
                            title = doc1.select("#container_head>p").text();
                            author = doc1.select("#author>p").text();
                            article = doc1.select("#text>p");
                            urlchild = doc1.select("#text>p>a");
                        } else {
                            continue;
                        }

                        if (urlchild.size() != 0) {
                            for (int i = 0; i < urlchild.size(); i++) {
                                arr1.add(i, urlchild.get(i).attr("href"));
                            }
                        }

                        if (article.size() != 0) {
                            for (int i = 0; i < article.size(); i++) {
                                list1.add(i, article.get(i).text());
                            }
                        } else {
                            continue;
                        }
                        break;
                    }
                    progressDialog.dismiss();
                    setTextView();
                }
            }).start();
        }
    }

    public void setTextView(){
        handler.post(new Runnable() {
            public void run() {
                //将获取到的数据更新到列表中

                textView1.append(title + "\n");
                textView2.append(author + "\n");
                int point = 0;
                for (int i = 0; i < article.size(); i++){
                    if(urlchild.size() != 0 && point<urlchild.size() && list1.get(i).equals(urlchild.get(point).text())){
                        if(arr1.get(point).startsWith("/upload")) {
                            textView3.append("       ");
                            textView3.append(Html.fromHtml("<a href=\"" + "http://www.youth.sdu.edu.cn" + arr1.get(point) + "\">" + urlchild.get(point).text() + "</a> "));
                            textView3.append("\n");
                        }
                        else{
                            textView3.append("       ");
                            textView3.append(Html.fromHtml("<a href=\"" + arr1.get(point) + "\">" + urlchild.get(point).text() + "</a> "));
                            textView3.append("\n");
                        }
                        point++;
                    }

                    else if(urlchild.size() != 0 && point<urlchild.size() &&
                            !list1.get(i).equals(urlchild.get(point).text()) && list1.get(i).contains( urlchild.get(point).text())){
                        textView3.append("       ");
                        textView3.append(list1.get(i) + "\n");
                        textView3.append("       ");
                        textView3.append(Html.fromHtml("<a href=\"" + arr1.get(point) + "\">" + urlchild.get(point).text() + "</a> "));
                        textView3.append("\n");
                        point++;
                    }

                    else{
                        textView3.append("       ");
                        textView3.append((list1.get(i) + "\n"));
                    }
                }
                textView3.setMovementMethod(LinkMovementMethod.getInstance());
                /*textView3.setAutoLinkMask(Linkify.EMAIL_ADDRESSES);*/
                /*if(urlchild.size() != 0) {
                    for (int i = 0; i < urlchild.size(); i++) {
                        textView3.append(Html.fromHtml("<a href=\"" + "http://www.youth.sdu.edu.cn" + arr1.get(i) + "\">" + urlchild.get(i).text() + "</a> "));
                        textView3.append("\n");
                    }
                }*/
                       /* int flags = Pattern.CASE_INSENSITIVE;
                        Pattern p = Pattern.compile(urlchild.get(i).text(), flags);
                        String uri;
                        if(arr1.get(i).startsWith("/upload")) {
                            uri = "http://www.youth.sdu.edu.cn" + arr1.get(i);
                        }
                        else {
                            uri = arr1.get(i);
                        }
                        Linkify.addLinks(textView3, p, uri);

                    }
                }*/
            }
        });
    }




}

